package com.playWright.demo;

import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
import org.junit.Test;

import java.nio.file.Paths;
import java.util.List;

public class PlaywrightDemo {

    private Playwright playwright;
    private Browser browser;
    private Page page;

    // 1️⃣ 启动浏览器
    public void launchBrowser() {
        playwright = Playwright.create();
        browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
        page = browser.newPage();
        System.out.println("浏览器启动成功，Chromium 路径：" + playwright.chromium().executablePath());
    }

    // 2️⃣ 关闭浏览器
    public void closeBrowser() {
        if (browser != null) browser.close();
        if (playwright != null) playwright.close();
        System.out.println("浏览器关闭！");
    }

    // 3️⃣ 打开网页
    public void navigateTo(String url) {
        page.navigate(url);
        System.out.println("已打开页面: " + url);
    }

    // 4️⃣ 获取页面标题
    public String getPageTitle() {
        return page.title();
    }

    // 5️⃣ 截取整个页面截图
    public void screenshotPage(String filePath) {
        page.screenshot(new Page.ScreenshotOptions()
                .setPath(Paths.get(filePath))
                .setFullPage(true));
        System.out.println("页面截图已保存: " + filePath);
    }

    // 6️⃣ 截取指定元素截图
    public void screenshotElement(String selector, String filePath) {
        page.locator(selector).screenshot(new Locator.ScreenshotOptions().setPath(Paths.get(filePath)));
        System.out.println("元素截图已保存: " + filePath);
    }

    // 7️⃣ 在百度搜索框输入并搜索
    public void searchBaidu(String keyword) {
        page.locator("#kw").fill(keyword);
        page.locator("#su").click();
        page.waitForLoadState(LoadState.DOMCONTENTLOADED);
        System.out.println("已搜索关键词: " + keyword);
    }

    // 8️⃣ 获取百度搜索结果标题列表
    public List<String> getBaiduResults() {
        List<String> results = page.locator("#content_left .result h3").allTextContents();
        return results;
    }
    @Test
    public void test() {

    }

    public static void main(String[] args) {
        PlaywrightDemo demo = new PlaywrightDemo();

        demo.launchBrowser();
        demo.navigateTo("https://www.baidu.com");

        System.out.println("首页标题: " + demo.getPageTitle());
        demo.screenshotPage("baidu_home.png");

        demo.searchBaidu("Playwright Java");
        demo.screenshotPage("baidu_search.png");
        demo.screenshotElement("#kw", "search_box.png");

        List<String> titles = demo.getBaiduResults();
        System.out.println("搜索结果前5条:");
        for (int i = 0; i < Math.min(5, titles.size()); i++) {
            System.out.println((i + 1) + ". " + titles.get(i));
        }

        demo.closeBrowser();
    }
//    public static void main(String[] args) {
//        try (Playwright playwright = Playwright.create()) {
//            Browser browser = playwright.chromium().launch(
//                    new BrowserType.LaunchOptions().setHeadless(true)
//            );
//            System.out.println(playwright.chromium().executablePath());
//            Page page = browser.newPage();
//            page.navigate("https://www.baidu.com");
//            page.screenshot(new Page.ScreenshotOptions()
//                    .setPath(Paths.get("screenshot.png"))
//                    .setFullPage(true)
//            );
//            browser.close();
//        }
//        System.out.println("截图完成！");
//    }
}